require 'will_paginate'

class SiteController < ApplicationController

    def index

	@newest = Picture.find(:all, {:order => "created_at DESC", :limit => 10})	
	
	@baza = Picture.find(:all, {:order => "score DESC", :limit => 100},:conditions => "votes > 0 AND score >0")
	
	
	
	
	@best=Array.new
	@value1=0
	@value2=0
	@chaos=rand(@baza.length*0.2)
	
	if @baza.length > 1
	
	    for i in (0..@baza.length-2)
		for i in (0..@baza.length-2)

		    #wersja geometryczna
		    #@value1 = Math.sqrt(@baza[i].score*@baza[i].score+@baza[i].votes*@baza[i].votes)
		    #@value2 = Math.sqrt(@baza[i+1].score*@baza[i+1].score+@baza[i+1].votes*@baza[i+1].votes) 
	
		    #wersja statystyczna
		    @value1 = (@baza[i].score-5.0)*@baza[i].votes
		    @value2 = (@baza[i+1].score-5.0)*@baza[i+1].votes
		    
	
		    #sortuje w porzadku rosnacym
		
		    if @value1 < @value2
			@swap = @baza[i]
			@baza[i] = @baza[i+1]
			@baza[i+1] = @swap
		    end
		end    
	    end

	    #ba�aganienie w tablicy

	    for i in (0..@chaos)
		@a=rand(@baza.length)
		@b=rand(@baza.length)
	    
	
		@swap = @baza[@a]
		@baza[@a] = @baza[@b]
		@baza[@b] = @swap    
	    end
    	
	    @best=@baza[0..@baza.length*0.5]
	else
	    if(@baza.length<1)
		@best=[]
	    else 
		@best = @baza
	    end
	end
	

  end

  def about
  end
  
  def profile
  	@user = User.find(session[:user_id])
  	@title = "Profil użytkownika #{@user.username}"
  end
  
  def user_edit
  	@title = "Edytuj swoje dane"
  	@user = User.find(session[:user_id])
  	if param_posted?(:user)
  		attribute = params[:attribute]
  		case attribute
  		when "email"
  			try_to_update @user, attribute
  		when "password"
  			if @user.correct_password?(params)
  				try_to_update @user, attribute
  			else
  				@user.password_errors(params)
  			end
  		end
  	end
  	@user.clear_password!
  end
  
  def gallery
    @user = User.find(params[:id])
    @pictures = Picture.paginate :page => params[:page], :order => 'created_at DESC', :conditions => "user_id = '#{params[:id]}'", :per_page => 20
  end
  
  def pictures
	@newest = Picture.paginate :page => params[:page], :order => 'created_at DESC', :per_page => 40
		
	@baza = Picture.find(:all, {:order => "score DESC", :limit => 100},:conditions => "votes > 0 AND score >0")
	

	@best=Array.new
	@value1=0
	@value2=0
	@chaos=rand(30)
	
	if @baza.length > 3 
	
	    for i in (0..@baza.length-2)
		for i in (0..@baza.length-2)

		    value1 = Math.sqrt(@baza[i].score*@baza[i].score+@baza[i].votes*@baza[i].votes)
		    value2 = Math.sqrt(@baza[i+1].score*@baza[i+1].score+@baza[i+1].votes*@baza[i+1].votes) 
	
	
		    #sortuje w porzadku rosnacym
		
		    if @value1 < @value2
			@swap = @baza[i]
			@baza[i] = @baza[i+1]
			@baza[i+1] = @swap
		    end
		end    
	    end

	    #ba�aganienie w tablicy

	    for i in (0..@chaos)
		@a=rand(@baza.length-1)
		@b=rand(@baza.length-1)
	    
	
		@swap = @baza[@a]
		@baza[@a] = @baza[@b]
		@baza[@b] = @swap    
	    end
    	
	    @best=@baza[0..10]
	
	end

  end

  def users
    @users = User.paginate :page => params[:page], :order => 'created_at DESC', :per_page => 20
  end
  
  def param_posted?(symbol)
	request.post? and params[symbol]
  end
  
  def register
  	@title = "old_Register"
  	if param_posted?(:user) #request.post? and params[:user] 
  		@user = User.new(params[:user])
  		if @user.save
  			session[:user_id] = @user.id
  			flash[:notice] = "Użytkownik #{@user.username} utworzony!"
  			redirect_to :action => "index"
  		end
  	end
  end
  
  def login
  	@title = "Logowanie do Aperture"
  	session[:user_id] = nil
  	if request.post? #param_posted?(:user)
  		user = User.authenticate(params[:username], params[:password])
  		if user
  			session[:user_id] = user.id
  			flash[:notice] = "Użytkownik #{user.username} zalogowany"
  			if user.is_admin
  				redirect_to(:action => "index", :controller => "users" )
  			else	
  				redirect_to(:action => "index")
  			end
  		else 
  			flash[:notice] = "Nieprawidłowy login lub hasło!"
			redirect_to(:action => "login")
  		end
	end  
  end
  
  def logout
  	session[:user_id] = nil
  	flash[:notice] = "Wylogowales sie!"
  	redirect_to :action => "index", :controller => "site"
  end
  
private
  
  def try_to_update(user, attribute)
  	if user.update_attributes(params[:user])
  	flash[:notice] = "Użytkownik zaktualizował #{attribute}."
  		redirect_to :action => "profile"
  	end
  end

end
